<template>
  <div class="mod-config">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="账号" prop="accountId">
        {{ this.dataForm.accountId }}
        <!-- <el-input v-model="dataForm.accountId" placeholder=""></el-input> -->
      </el-form-item>
      <!-- <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder=""></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="角色" prop="role">
      <el-input v-model="dataForm.role" placeholder="0--管理员，1--发起者，2--投票者"></el-input>
    </el-form-item> -->
      <el-form-item label="状态" prop="status">
        {{ this.dataForm.status == 1 ? "活跃中" : "封禁中" }}
      </el-form-item>
      <el-form-item label="昵称" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="昵称"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <single-upload v-model="dataForm.avatar"></single-upload>
        <!-- <el-input placeholder="头像地址"></el-input> -->
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio v-model="dataForm.gender" :label="1">男</el-radio>
        <el-radio v-model="dataForm.gender" :label="0">女</el-radio>
        <el-radio v-model="dataForm.gender" :label="-1">保密</el-radio>
        <!-- <el-input
          v-model="dataForm.gender"
          placeholder="性别，0--女，1--男，-1 -- 保密"
        ></el-input> -->
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <!-- <el-input
          v-model="dataForm.age"
          placeholder="年龄 -1 -- 保密"
        ></el-input> -->
        <el-select
          v-model="dataForm.age"
          filterable
          allow-create
          default-first-option
          placeholder="请输入年龄"
        >
          <el-option
            label="保密"
            :value=-1
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" @click="dataFormSubmit()">确认修改</el-button>
    </span>
  </div>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload.vue";
export default {
  components: { SingleUpload },
  data() {
    return {
      dataForm: {
        id: 0,
        accountId: "",
        password: "",
        status: "",
        avatar: "",
        userName: "",
        gender: "",
        age: "",
      },
      dataRule: {
        accountId: [{ required: true, message: "不能为空", trigger: "blur" }],
        // password: [{ required: true, message: "不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "0--审核中，1--active，2--封禁中，-1--已注销不能为空",
            trigger: "blur",
          },
        ],
        avatar: [
          { required: true, message: "头像地址不能为空", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "昵称不能为空", trigger: "blur" },
        ],
        gender: [
          {
            required: true,
            message: "性别，0--女，1--男，-1 -- 保密不能为空",
            trigger: "blur",
          },
        ],
        age: [
          {
            required: true,
            message: "年龄 -1 -- 保密不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.init(localStorage.getItem("userId"));
  },
  methods: {
    init(id) {
      this.$http({
        url: this.$http.adornUrl(`/voteOnline/user/info/${id}`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataForm.id = data.user.id;
          this.dataForm.accountId = data.user.accountId;
          this.dataForm.status = data.user.status;
          this.dataForm.avatar = data.user.avatar;
          this.dataForm.userName = data.user.userName;
          this.dataForm.gender = data.user.gender;
          this.dataForm.age = data.user.age;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/voteOnline/user/update`),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id,
              accountId: this.dataForm.accountId,
              status: this.dataForm.status,
              avatar: this.dataForm.avatar,
              userName: this.dataForm.userName,
              gender: this.dataForm.gender,
              age: this.dataForm.age,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.init(localStorage.getItem("userId"));
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
